﻿using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SkyJukebox
{
    public partial class MiniPlayer : Form
    {
        private string _lastPlaylist;

        readonly SplashScreen _spl = new SplashScreen();

        private const int WmNclbuttondown = 0xA1;
        private const int HtCaption = 0x2;

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();

        public MiniPlayer()
        {
            Hide();
            var splashthread = new Thread(_spl.ShowSplashScreen) { IsBackground = true };
            splashthread.Start();

            InitializeComponent();
        }

        private void MiniPlayer_Load(object sender, EventArgs e)
        {
            var t = Util.LoadStuff();
            Program.BgPlayer.PlaybackEvent += bgPlayer_PlaybackEvent;
            Location = Util.GetCornerLocation(this);

            if (t < 1500)
                Thread.Sleep(1500 - (int)t);
            else
                Thread.Sleep(500);
            Show();
            _spl.CloseSplashScreen();
            Activate();
            if (!Program.Settings.ShowPlaylistEditorOnStartup) return;
            var pe = new PlaylistEditor();
            pe.Show();
        }

        private void playerControlToolStrip_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            ReleaseCapture();
            SendMessage(Handle, WmNclbuttondown, HtCaption, 0);
        }

        private void openPlaylistButton_Click(object sender, EventArgs e)
        {
            var ofdiag = new OpenFileDialog { Filter = "Any M3U Playlist (*.m3u*)|*.m3u*", Multiselect = false };
            if (ofdiag.ShowDialog() != DialogResult.OK) return;
            Program.BgPlayer.Playlist = new Playlist(ofdiag.FileName);
            _lastPlaylist = ofdiag.FileName;
            Text = "Playlist: " + ofdiag.FileName;
        }

        private void MiniPlayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.Settings.LastWindowLocation = Location;
            Program.Settings.SaveToXml();
            Program.BgPlayer.Dispose();
            Program.BgPlayer = null;
            controlNotifyIcon.Visible = false;
        }

        private void playPauseButton_Click(object sender, EventArgs e)
        {
            switch (Program.BgPlayer.Status)
            {
                case PlaybackStatus.Stopped:
                    Program.BgPlayer.Play();
                    break;
                case PlaybackStatus.Paused:
                    Program.BgPlayer.Resume();
                    break;
                default:
                    Program.BgPlayer.Pause();
                    break;
            }
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Previous();
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Next();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Stop();
        }

        void bgPlayer_PlaybackEvent(object sender, PlaybackEventArgs e)
        {
            if (InvokeRequired)
            {
                var res = BeginInvoke((Action<PlaybackEventArgs>)UpdateScreen, e);
                EndInvoke(res);
            }
            else
                UpdateScreen(e);
        }

        private void UpdateScreen(PlaybackEventArgs e)
        {
            Text = Util.FormatHeader(Program.BgPlayer.Playlist[e.NewTrackId], Program.Settings.HeaderFormat);
            controlNotifyIcon.BalloonTipText = "Now Playing: " + e.NewTrackName;
            if (!Visible)
                controlNotifyIcon.ShowBalloonTip(2000);
        }

        private void minimizeButton_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void controlNotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Show();
        }

        private void shuffleButton_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Shuffle = !Program.BgPlayer.Shuffle;
        }

        private void loopButton_Click(object sender, EventArgs e)
        {
            switch (Program.BgPlayer.LoopType)
            {
                case LoopType.None:
                    Program.BgPlayer.LoopType = LoopType.Single;
                    break;
                case LoopType.Single:
                    Program.BgPlayer.LoopType = LoopType.All;
                    break;
                default:
                    Program.BgPlayer.LoopType = LoopType.None;
                    break;
            }
        }

        private void playlistEditorButton_Click(object sender, EventArgs e)
        {
            Program.PlaylistEditorInstance.Show();
        }

        private void exitToolStripButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void showMiniPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Visible = true;
        }

        private void showPlaylistEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.PlaylistEditorInstance.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void playPauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (Program.BgPlayer.Status)
            {
                case PlaybackStatus.Stopped:
                    Program.BgPlayer.Play();
                    break;
                case PlaybackStatus.Paused:
                    Program.BgPlayer.Resume();
                    break;
                default:
                    Program.BgPlayer.Pause();
                    break;
            }
        }

        private void previousToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Previous();
        }

        private void nextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Next();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.BgPlayer.Stop();
        }
    }
}
